﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_doctors : System.Web.UI.Page
{
    protected Administrator admin;
    protected int SortID;
    private int TeamGuid;
    private int CategoryGuid;
    protected void Page_Load(object sender, EventArgs e)
    {
        admin = Administrator.Check();
        if (admin == null)
        {
            Session.Add(Params.SystemError, "请先登录");
            Response.Redirect(Params.RootHttp + "default.aspx");
        }
        if (Request.Params["t"] == null)
        {
            this.TeamGuid = 0;
        }
        else
        {
            this.TeamGuid = Convert.ToInt32(Server.UrlDecode(Request.Params["t"]));
        }
        if (Request.Params["c"] == null)
        {
            this.CategoryGuid = -1;
        }
        else
        {
            this.CategoryGuid = Convert.ToInt32(Server.UrlDecode(Request.Params["c"]));
        }
        if (!Page.IsPostBack)
        {
            this.MyDataBind();
        }
    }

    public void MyDataBind()
    {
        this.conNav.Title = "导师列表";
        this.SortID = 1;
        this.BindCategory();
        this.BindTeam();
        this.BindDoctors();
    }
    protected void ddTeam_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect(Params.AdminFolderHttp+"doctors.aspx?t="+Server.UrlEncode(this.ddTeam.SelectedValue)+"&c="+Server.UrlEncode(this.CategoryGuid.ToString()));
    }
    protected void ddCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect(Params.AdminFolderHttp + "doctors.aspx?t="+Server.UrlEncode(this.TeamGuid.ToString())+"&c="+Server.UrlEncode(this.ddCategory.SelectedValue));
    }

    private void BindTeam()
    {
        this.ddTeam.DataSource = Team.Teams(true);
        this.ddTeam.DataBind();
        this.ddTeam.SelectedValue = this.TeamGuid.ToString();

        this.ddSztd.DataSource = Team.Teams(false);
        this.ddSztd.DataBind();
    }

    private void BindCategory()
    {
        this.ddCategory.DataSource = Category.Categorys(true);
        this.ddCategory.DataBind();
        this.ddCategory.SelectedValue = this.CategoryGuid.ToString();

        this.ddDslx.DataSource = Category.Categorys(false);
        this.ddDslx.DataBind();
    }

    private void BindDoctors()
    {
        string sql = "select u.Guid from Users u Join Doctor d on u.Guid=d.UserGuid where u.UserType='"+LwUser.DoctorType+"'";
        this.SortID = 1;
        ArrayList ord = new ArrayList();
        if (this.TeamGuid != 0)
        {
            ord.Add("d.TeamGuid="+this.TeamGuid.ToString());
        }
        if (this.CategoryGuid != -1)
        {
            ord.Add("d.CategoryGuid=" + this.CategoryGuid.ToString());
        }
        sql =sql+ Fun.GenerateOrderSQL(ord,false,true)+" order by u.Guid asc";
        DataTable dt = Fun.CreateDt(sql);
        this.repDoctors.DataSource = dt;
        this.repDoctors.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string uid = this.tbID.Text.Trim();
        string n = this.tbName.Text.Trim();
        string email = this.tbEmail.Text.Trim();
        bool exp = this.cbExpert.Checked;
        int tg = Convert.ToInt32(this.ddSztd.SelectedValue);
        int cg = Convert.ToInt32(this.ddDslx.SelectedValue);
        if (uid == "" || n == "")
        {
            this.info.SetMessage("姓名和ID不能为空！");
            return;
        }
        string tip = "";
        int g = Doctor.Add(uid, n, email, tg, cg, exp, ref tip);
        if (g == 0)
        {
            Session.Add(Params.SystemError, tip);
            Response.Redirect(Request.RawUrl);
        }
        Session.Add(Params.SystemError, "添加成功！");
        Response.Redirect(Params.AdminFolderHttp + "doctors.aspx?guid="+g.ToString());
    }
}
